package com.ruoyi.mateTemplate.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateTemplate.domain.MateTemplate;
import com.ruoyi.mateTemplate.service.MateTemplateService;
import com.ruoyi.material.domain.MateProductDevice;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.product.domain.Product;
import com.ruoyi.product.domain.ProductRelation;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateTemplate")
public class    MateTemplateController extends BaseController {

    @Autowired
    MateTemplateService mateTemplateService;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    /**
     * 新增保存领料单
     */
    @PostMapping("/insertTemplate")
    @ResponseBody
    public AjaxResult insertTemplate(@RequestBody MateTemplate mateTemplate) {
        int a = mateTemplateService.insertTemplate(mateTemplate);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @PostMapping("/createTemplate")
    @ResponseBody
    public AjaxResult createTemplate(@RequestBody MateTemplate mateTemplate) {
        mateTemplateService.createTemplate(mateTemplate);
        return AjaxResult.success("生成模版成功");
    }

    @GetMapping("/selectTemplate")
    public List<ProductRelation> selectTemplate(@RequestParam(value = "productId", required = false) Long productId) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("productId", productId);
        resMap.put("deptId", SecurityUtils.getDeptId());
        List<ProductRelation> productRelations = mateTemplateService.selectTemplate(resMap);
        return productRelations;
    }

    @GetMapping("/selectMateTemplate")
    public Map<String, Object> selectMateTemplate(@RequestParam(value = "productId", required = false) Long productId) {
        Map<String, Object> resMap = mateTemplateService.selectMateTemplate(productId);
        return resMap;
    }

    @GetMapping("/selectLastRecord")
    public Map<String, Object> selectLastRecord(@RequestParam(value = "productId", required = false) Long productId) {
        Map<String, Object> resMap = mateTemplateService.selectLastRecord(productId);
        return resMap;
    }

    @GetMapping("/selectProduct")
    public List<Product> selectProduct() {
        List<Product> products = productMapper.selectProducts(SecurityUtils.getDeptId());
        return products;
    }

    @GetMapping("/selectSignature")
    public List<SysUser> selectSignature() {
        List<SysUser> sysUsers = sysUserMapper.selectSalesmanList(SecurityUtils.getDeptId());
        return sysUsers;
    }
}
